package cmd

import (
	"github.com/spf13/cobra"
	"log"
	"longmen/server/config/global"
	"os"
	"os/signal"
	"runtime/debug"
	"syscall"
)

var rootCmd = &cobra.Command{
	Use:   "consumers",
	Short: "所有的消费者均",
	Long:  `包括统计数据等`,
	PreRun: func(cmd *cobra.Command, args []string) {

	},
}

func Execute() {
	// 尝试捕获panic并保存到错误中
	defer func() {
		if r := recover(); r != nil {
			log.Println(r)
			debug.PrintStack()
		}
	}()
	//rootCmd.AddCommand(test)
	rootCmd.AddCommand(statisticCli)
	rootCmd.AddCommand(txChatCli)
	rootCmd.AddCommand(accountCli)
	rootCmd.AddCommand(chanStatsCli)
	rootCmd.AddCommand(activityCli)
	rootCmd.AddCommand(txChatHistoryCli)
	rootCmd.AddCommand(txChatAnonymousCli)
	rootCmd.AddCommand(txPvtChatInitCli)
	rootCmd.AddCommand(txFansGroupInit)

	if err := rootCmd.Execute(); err != nil {
		os.Exit(1)
	}
}

func gracefulShutdown() {
	//创建监听退出chan
	c := make(chan os.Signal, 1)
	//监听指定信号 ctrl+c kill
	signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
	for s := range c {
		switch s {
		case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
			global.Log.Info("退出信号： ", s)
			os.Exit(0)
		default:
			global.Log.Info("未知信号", s)
		}
	}
}
